// pages/personal/personal.js
import {redirectIndex} from "../../utils/init";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {
      name: '',
      post: '',
      company: '',
      avatar: '',
      userRowId: '',
    },

    // navList: [
    //   { icon: 'icon-tongxunlu', name: '通讯录', url: '', },
    //   { icon: 'icon-richeng2', name: '日程', url: '', },
    //   { icon: 'icon-yibiaopan', name: '仪表盘', url: '../dashboard/dashboard', },
    // ],
    navList: [
      { icon: 'icon-tongxunlu', name: '通讯录', url: '/pages/general_entity/general_entity?entity=all_employee_inf', },
      { icon: 'icon-yibiaopan_wode', name: '仪表盘', url: '../dashboard/dashboard', },
      { icon: 'icon-richeng1', name: '日程', url: '', },
    ],
    list: [
      { icon: 'icon-wodeshenpi', name: '我的审批', url: '/DIYPages/approval/approval', },
      { icon: 'icon-woderenwu', name: '我的任务', url: '', },
      { icon: 'icon-wodeguanzhu', name: '我的关注', url: '', },
      { icon: 'icon-wodehuati', name: '我的话题', url: '', },
      { icon: 'icon-bangzhuzhongxin', name: '帮助中心', url: '', },
      { icon: 'icon-zhanghaojiebang', name: '账号解绑', url: 'unbind', },
      { icon: 'icon-tuichudenglu', name: '退出系统', url: 'logout', },
    ],
  },

  // 选择头像 小程序用户头像昵称获取规则调整公告：https://developers.weixin.qq.com/community/develop/doc/00022c683e8a80b29bed2142b56c01
  onChooseAvatar(e) {
    let { avatarUrl } = e.detail,
        { userInfo, } = this.data

    // 小程序选择的图片临时本地路径转为base64
    wx.$util.base64(avatarUrl).then(res => {
      userInfo.avatar = res
      this.setData({
        userInfo,
      })
      wx.setStorageSync('avatar', res)
    })
  },
  // nav 跳转
  navTo(e) {
    let url = e.currentTarget.dataset.url
    if(!url) return
    if(url === 'logout') {  // 退出登录
      this.logout()
    } else if(url === 'unbind') { // 账号解绑
      this.unbindAcct()
    } else if(url.indexOf('dashboard') > -1) {
      wx.switchTab({
        url: url,
      })
    } else {
      wx.navigateTo({
        url: url,
      })
    }
  },
  // 退出登录
  logout() {
    let info = wx.getAccountInfoSync(),
      appid = info && info.miniProgram && info.miniProgram.appId;

    //方通渣土沙石、方通渣土挖运、友程企业经营管理系统
    if (['wxa6bb52a95207e8ab','wx6c99b9c67ec88087','wx8aa2d2af63243c3a'].includes(appid)){
      redirectIndex();
      return
    }
    wx.showModal({
      // title: '确认退出登录？',
      content: '确认退出登录？',
      success: (res) => {
        if(res.confirm) {
          // 清空登录信息相关cookie，跳回登录页
          let rlatKey = ['refresh', 'token', 'company_name', 'company_id', 'user_name', 'user_id', 'role_id', 'is_platfrom', 'is_agent', 'userLevel', ]
          rlatKey.forEach(key => wx.removeStorageSync(key))
          wx.reLaunch({
            url: '/DIYPages/login/login',
          })
        }
      },
    })
  //  231102 end
  },
  // 解绑账号
  unbindAcct() {
    wx.showModal({
      title: '确定要解除微信账号绑定吗？',
      content: '解除微信账号绑定后，该账号将不能通过微信用户进行一键登录',
      success: (res) => {
        if(res.confirm) {
          wx.$post(1, 'WXMiniProgram/update_miniprogram_unbind', {}).then(res => {
            if(res.errcode !== 0) return
            wx.$toast('解绑成功！')
          })
        }
      },
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.$setBarTitle();

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      userInfo: {
        name: wx.getStorageSync('user_name') || wx.getStorageSync('user_id'),
        post: '',
        company: wx.getStorageSync('company_name'),
        avatar: wx.getStorageSync('avatar'),
        userRowId: wx.getStorageSync('employee_row_id'),
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
})
